var options = {
	ignore: ":hidden",
	messages: {},
	rules: {}
};

var WValidate = {

    init:function(formId , data){
		data = data || options;
		$("#" + formId).validate({
			ignore: data.ignore || options.ignore,
            submitHandler:function(form){
            	form.submit();
            },
            highlight: function(element, errorClass) {
    			$(element).addClass(errorClass);
    			$(element.form).find("span[for=" + element.id + "]").addClass(errorClass);
    		},
    		unhighlight: function(element, errorClass) {
    			$(element).removeClass(errorClass);
    			$(element.form).find("span[for=" + element.id + "]").removeClass(errorClass); 
    		},
    		errorPlacement:function(error,element) {
    			if(data.showTip){
    				var message = $(element).next();
    				if(message[0]){
    					$(message[0]).html(error);
    				}
    			}
    		},
    		rules : data.rules || options.rules,
    		messages: data.messages || options.messages
		});	
	},
	
	initAjaxSubmit:function(formId , data, submitMethod){
		data = data || options;
		$("#" + formId).validate({
			ignore: data.ignore || options.ignore,
            submitHandler:function(form){
            	submitMethod();
            	return false;
            },
            highlight: function(element, errorClass) {
    			$(element).addClass(errorClass);
    			$(element.form).find($.parseHTML("span[for=" + element.id + "]", document, true)).addClass(errorClass);
    		},
    		unhighlight: function(element, errorClass) {
    			$(element).removeClass(errorClass);
    			if($(element).next().hasClass("valid-wrap")){
    				$(element).next().remove();
    			}
    		},
    		errorPlacement:function(error,element) {
    			if(data.showTip){
    				if(!$(element).next().hasClass("valid-wrap")){
        				var f = $(element).offset();
            	        f = {
            	            oLeft: f.left >= 0 ? f.left - $(element).offsetParent().offset().left: 155,
            	            oTop: f.left >= 0 ? f.top - $(element).offsetParent().offset().top: 4,
            	            width: f.left >= 0 ? 28 : 228
            	        };
            			var appendDiv = $('<div class="valid-wrap"><div><img src="/img/checked.gif" alt=""  class="float-left" style="margin:1px 4px 0 0"><span class="float-left">'+error+'</span></div><div class="valid-arrow"></div></div>');
            			appendDiv.addClass("valid-inner").css({
            	            position: "absolute",
            	            zIndex: 7001,
            	            display: "block",
            	            visibility: "visible"
            	        });
            			appendDiv.css({
        	                width:"auto",
        	                top: $(element).outerHeight() + f.oTop,
        	                left: f.oLeft
            	         });
            			$(element).after(appendDiv);
        			}
    				$(element).next().html(error);
    			}
    		},
    		rules : data.rules || options.rules,
    		messages: data.messages || options.messages
		});	
	},
	
	initIncludeMuti:function(formId , data){
		data = data || options;
		$("#" + formId).validate({
			ignore: data.ignore || options.ignore,
            submitHandler:function(form){
            	form.submit();
            },
            highlight: function(element, errorClass) {
    			$(element).addClass(errorClass);
    		},
    		unhighlight: function(element, errorClass) {
    			$(element).removeClass(errorClass);
    			if($(element).next().hasClass("valid-wrap")){
    				$(element).next().remove();
    			}
    		},
    		errorPlacement:function(error,element) {
    			if(data.showTip){
    				if(!$(element).next().hasClass("valid-wrap")){
        				var f = $(element).offset();
            	        f = {
            	            oLeft: f.left >= 0 ? f.left - $(element).offsetParent().offset().left: 155,
            	            oTop: f.left >= 0 ? f.top - $(element).offsetParent().offset().top: 4,
            	            width: f.left >= 0 ? 28 : 228
            	        };
            			var appendDiv = $('<div class="valid-wrap"><div><img src="/img/checked.gif" alt=""  class="float-left" style="margin:1px 4px 0 0"><span class="float-left">'+error+'</span></div><div class="valid-arrow"></div></div>');
            			appendDiv.addClass("valid-inner").css({
            	            position: "absolute",
            	            zIndex: 7001,
            	            display: "block",
            	            visibility: "visible"
            	        });
            			appendDiv.css({
        	                width:"auto",
        	                top: $(element).outerHeight() + f.oTop,
        	                left: f.oLeft
            	         });
            			$(element).after(appendDiv);
        			}
    				$(element).next().html(error);
    			}
    		},
    		rules : data.rules || options.rules,
    		messages: data.messages || options.messages
		});	
	}
};
